import axios from '../../../utils/http'
import XLSX from 'xlsx'
import Cookies from 'js-cookie'
import {baseURL} from '../../../utils/http'

// 查询学期
export async function getTermList() {
  const {
    data
  } = await axios.get('/bac/term/getTermList')
  return data
}


// 查询周次 /term/weekList/ /iseInspection/analysisAll
export async function getWeekList(parameter) {
  const {
    data
  } = await axios.get('/bac/weekDate/getWeekListByTerm?' + parameter)
  return data
}


// 获取请假表
export async function getLeaveList(parameter) {
  const {
    data
  } = await axios.get('/dormNight/dormNightLeave' + parameter)
  return data
}
// 修改 提交
export async function addLeaveDetails( endTime, id, remark,  startTime) {
  const {
    data
  } = await axios.put('/dormNight/dormNightLeave', {
    endTime: endTime,
    id: id,
    remark: remark,
    startTime: startTime,
  })
  return data
}
// 导入  或  新增请假表    Excel
export async function updataLeaveData(params) {
  const {
    data
  } = await axios.post('/dormNight/dormNightLeave/uploadExcel',
    params
  )
  return data
}
//
export async function updataLeaveDetails( ) {
  const {
    data
  } = await axios.put('/dormNight/dormNightLeave', {





  })
  return data
}

// 删除值班情况   /iseSchedule/schedule
export async function deleteLeaveDetails(scheduleIds) {
  const {
    data
  } = await axios.delete('/dormNight/dormNightLeave', {
    data: scheduleIds


  })
  return data
}

// Excel导入数据处理
export function excel(file) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader()
    reader.readAsArrayBuffer(file)
    reader.onerror = e => {
      reject(e)
    }
    reader.onload = e => {
      const excelData = e.target.result
      const wb = XLSX.read(excelData, {
        type: 'array'
      })
      const firstSheetName = wb.SheetNames[0]
      const worksheet = wb.Sheets[firstSheetName]
      const columns = processHeader(worksheet)
      const tableData = XLSX.utils.sheet_to_json(columns)
      // if (!tableData[0].学期 && !tableData[0].周次) {
      //   resolve(false)
      // }
      resolve(tableData)
    }
  })

}

//
// /dormNight/spotLog   批量删除值班计划 deleteDistributionData
export async function deleteLeaveData(scheduleIds) {
  const {
    data
  } = await axios.delete('/dormNight/dormNightLeave', {
    data: scheduleIds,
  })
  return data
}
// 格式化Excel数据 表头
function processHeader(sheet) {
  const range = XLSX.utils.decode_range(sheet['!ref'])
  const C = range.e.c
  for (let i = 0; i <= C; i++) {
    sheet[String.fromCharCode(65 + i) + 1] = sheet[String.fromCharCode(65 + i) + 2]
    delete sheet[String.fromCharCode(65 + i) + 2]
  }
  return sheet
}

// 添加导入Excel数据  /dormNight/dormNightLeave/uploadExcel
//导入或新增抽查计划(导入使用Excel)
export async function addExceldata(excelData) {
  const {
    data
  } = await axios.post('/dormNight/dormNightLeave/uploadExcel', excelData)
  return data
}

// 导出Excel数据
export function getExcel() {
  fetch(baseURL+'/dormNight/dormNightLeave/download', {
      method: 'GET',
      headers: {
        'JHC-Authorization': Cookies.get('token')
      },
    })
    .then(res => {
      return res.blob()
    })
    .then(data => {
      const blobUrl = window.URL.createObjectURL(data)
      download(blobUrl)
    })
}
//getselfExcel自习表的导出
export function getselfExcel() {
  fetch(baseURL+'study/download', {
      method: 'GET',
      headers: {
        'JHC-Authorization': Cookies.get('token')
      },
    })
    .then(res => {
      return res.blob()
    })
    .then(data => {
      const blobUrl = window.URL.createObjectURL(data)
      download(blobUrl)
    })
}
// downloadShuttering
export function downloadShuttering() {
  fetch(baseURL+'study/downloadTemplate', {
      method: 'GET',
      headers: {
        'JHC-Authorization': Cookies.get('token')
      },
    })
    .then(res => {
      return res.blob()
    })
    .then(data => {
      const blobUrl = window.URL.createObjectURL(data)
      downloads(blobUrl)
    })
}
export function downloadWorkShuttering() {
  fetch(baseURL+'/dormNight/dormNightLeave/template', {
      method: 'GET',
      headers: {
        'JHC-Authorization': Cookies.get('token')
      },
    })
    .then(res => {
      return res.blob()
    })
    .then(data => {
      const blobUrl = window.URL.createObjectURL(data)
      downloadWork(blobUrl)
    })
}

function download(blobUrl) {
  const a = document.createElement('a')
  a.download = '学生请假管理表' + '.xlsx'

  a.href = blobUrl
  a.click()
}

function downloads(blobUrl) {
  const a = document.createElement('a')
  a.download = '自习安排表模板' + '.xlsx'
  a.href = blobUrl
  a.click()
}

function downloadWork(blobUrl) {
  const a = document.createElement('a')
  a.download = '学生请假模板' + '.xlsx'
  a.href = blobUrl
  a.click()
}
// 查询学生列表的模糊查询  /teacher/teacherListByLikeName
export async function getStudentListByLikeName(parameter) {
  const {
    data
  } = await axios.get('/cms/student/userListByLikeName',{
    params:{
      name: parameter
    }
  })
  return data
}
